package com.sprinboot_vue.blog_springboot.controller;

import com.sprinboot_vue.blog_springboot.entity.User;
import com.sprinboot_vue.blog_springboot.repository.UserRepository;
import com.sprinboot_vue.blog_springboot.result.Result;
import com.sprinboot_vue.blog_springboot.result.ResultInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Stream;

/**
 * @author Thomas
 * @version 1.0
 * @date 2021/11/20 22:32
 */
@RestController
@RequestMapping("/user")
@Api(tags = "后台登录用户操作模块")
public class UserHandler {

    //自动装载
    @Autowired
    private UserRepository userRepository;

    //添加用户操作
    @ApiOperation("添加用户")
    @PostMapping("/saveUser")
    public Result addUser(@RequestBody User user){
        //添加之前必须检测一下该用户时候存在
        Optional<User> byId = userRepository.findById(user.getId());
        if(!byId.isPresent()){ //如果没找到这个账号的用户，说明可以添加该用户！！！
            try{
                User save = userRepository.save(user);
                //成功不需要返回data数据
                return Result.success().codeAndmessage(ResultInfo.SUCCESS);
            }catch (Exception e){
                return Result.error().codeAndmessage(ResultInfo.ERROR).date("error",e);
            }
        }else{//已经存在此账号了！！！
            return Result.error().codeAndmessage("404","已经存在此账号了！");
        }
    }

    //修改用户操作
    @ApiOperation("修改用户的昵称或者邮件")
    @PutMapping("/updateUser")
    public Result updateUser(@RequestBody User user){
        try{
            User save = userRepository.save(user);
            //成功不需要返回data数据
            return Result.success().codeAndmessage(ResultInfo.SUCCESS).date("data",save);
        }catch (Exception e){
            return Result.error().codeAndmessage(ResultInfo.ERROR).date("error",e);
        }
    }

    //删除用户操作
    @ApiOperation("通过id(账号主键)删除用户")
    @DeleteMapping("/deleteUser/{id}")
    public Result deleteUser(@PathVariable("id") String id){
        try{
            userRepository.deleteById(id);
            //成功不需要返回data数据
            return Result.success().codeAndmessage(ResultInfo.SUCCESS);
        }catch (Exception e){
            return Result.error().codeAndmessage(ResultInfo.ERROR).date("error",e);
        }
    }

    //查询用户操作(分页查询)
    @ApiOperation("分页查询用户操作")
    @GetMapping("/findAll/{page}/{pagesize}")
    public  Result findAll(@PathVariable("page") int page,@PathVariable("pagesize") int pagesize){
        Pageable pageable = PageRequest.of(page - 1, pagesize);
        Page<User> all = userRepository.findAll(pageable);
        List<User> content = all.getContent();
        for (int i = 0; i < content.size(); i++) {
            content.get(i).setPassword("null");
        }
        long totalElements = all.getTotalElements();
        return Result.success().codeAndmessage(ResultInfo.SUCCESS).date("data",content).date("totalElements",totalElements);
    }

    //验证用户 用户进行密码登录
    @ApiOperation("账号和密码进行验证登录")
    @GetMapping("/findByid/{id}/{password}")
    public Result fondByid(@PathVariable("id") String id,@PathVariable("password") String pwd){
        Optional<User> byId = userRepository.findById(id);
        if(!byId.isPresent()){
            return Result.error().codeAndmessage("200","数据库无此账号！");
        }else{
            if(byId.get().getPassword().equals(pwd)){
                return Result.success().codeAndmessage(ResultInfo.SUCCESS).date("Login_user",byId);
            }else{
                return Result.error().codeAndmessage("200","密码错误！");
            }
        }
    }
}
